﻿#include "MessageController.h"
#include "SystemInfoController.h"
#include "TransactionInfoController.h"
#include "ChatController.h"

MessageController::MessageController(){}

MessageController::~MessageController(){}

void MessageController::viewDidLoad(){
	CAView* view=this->getView();
	view->setColor(ccc4(77, 77, 77, 255));
	CAView* subview=CAView::createWithLayout(DLayout(DHorizontalLayoutFill, DVerticalLayout_T_H(0, 100)), themeColor);
	CALabel* title=CALabel::createWithLayout(DLayout(DHorizontalLayout_W_C(160, 0.51), DVerticalLayout_H_C(50, 0.5)));
	title->setText("消息");
	title->setFontSize(40);
	title->setColor(CAColor4B::WHITE);
	subview->addSubview(title);
	view->addSubview(subview);
	//系统通知
	float width=(view->getBounds().size.width-40)/2;
	subview=CAView::createWithLayout(DLayout(DHorizontalLayout_L_W(10, width), DVerticalLayout_T_H(110, 116)), CAColor4B::WHITE);
	CAImageView* img=CAImageView::createWithLayout(DLayout(DHorizontalLayout_L_W(20, 100), DVerticalLayout_H_C(100, 0.5)));
	img->setImage(CAImage::create("img/systeminformation.png"));
	subview->addSubview(img);
	CALabel* label=CALabel::createWithLayout(DLayout(DHorizontalLayout_L_W(130, 170), DVerticalLayout_H_C(50, 0.5)));
	label->setText("系统通知");
	label->setFontSize(40);
	label->onTouchBegan([](CATouch* touch, CAEvent* event){
		getRootWindow()->pushViewController(SystemInfoController::create(), true);

		return true;
	});
	subview->addSubview(label);
	view->addSubview(subview);
	//交易通知
	subview=CAView::createWithLayout(DLayout(DHorizontalLayout_R_W(10, width), DVerticalLayout_T_H(110, 116)), CAColor4B::WHITE);
	img=CAImageView::createWithLayout(DLayout(DHorizontalLayout_L_W(20, 100), DVerticalLayout_H_C(100, 0.5)));
	img->setImage(CAImage::create("img/transactionnotification.png"));
	subview->addSubview(img);
	label=CALabel::createWithLayout(DLayout(DHorizontalLayout_L_W(130, 170), DVerticalLayout_H_C(50, 0.5)));
	label->setText("交易通知");
	label->setFontSize(40);
	label->onTouchBegan([](CATouch* touch, CAEvent* event){
		getRootWindow()->pushViewController(TransactionInfoController::create(), true);

		return true;
	});
	subview->addSubview(label);
	view->addSubview(subview);
	/************私信消息************/
	//创建CATableView
	table=CATableView::createWithLayout(DLayout(DHorizontalLayout_L_R(10, 10), DVerticalLayout_T_B(230, 0)));
	//是否开启cell选择
	table->setAllowsSelection(true);
	//是否可以多选cell
	table->setAllowsMultipleSelection(false);
	//是否显示滚动条
	table->setShowsScrollIndicators(false);
	//设置尾部的高度
	table->setTableFooterHeight(100);
	this->getView()->addSubview(table);
	//设置加载的cell个数
	table->onNumberOfRowsAtIndexPath([&](unsigned int section){
		return datas.size();
	});
	//设置cell高度
	table->onCellHeightAtIndexPath([](unsigned int section, unsigned int row){
		return 130;
	});
	//设置cell显示的内容
	table->onCellAtIndexPath([&](DSize cellSize, unsigned int section, unsigned int row){
		CATableViewCell* cell=dynamic_cast<CATableViewCell*>(table->dequeueReusableCellWithIdentifier("message_cell"));
		if(cell==nullptr){
			cell=CATableViewCell::create("messages_cell");
			CAView* subview=cell->getContentView();

			CAImageView* portrait=CAImageView::createWithLayout(DLayout(DHorizontalLayout_L_W(10, 85), DVerticalLayout_H_C(85, 0.5)));
			portrait->setTag(100);
			subview->addSubview(portrait);
			CAImageView* maskView=CAImageView::createWithLayout(DLayout(DHorizontalLayout_L_W(10, 85), DVerticalLayout_H_C(85, 0.5)));
			maskView->setImage(CAImage::create("img/mask_image.png"));
			subview->addSubview(maskView);

			CALabel* name=CALabel::createWithLayout(DLayout(DHorizontalLayout_L_R(100, 200), DVerticalLayout_H_C(50, 0.3)));
			name->setTag(200);
			name->setFontSize(50);
			name->setTextAlignment(CATextAlignment::Left);
			name->setVerticalTextAlignmet(CAVerticalTextAlignment::Center);
			subview->addSubview(name);

			CAButton* num=CAButton::createWithLayout(DLayout(DHorizontalLayout_R_W(10, 67), DVerticalLayout_H_C(42, 0.35)), CAButton::Type::Custom);
			num->setTag(300);
			num->setTitleFontSize(32);
			num->setTitleColorForState(CAControl::State::Normal, CAColor4B::WHITE);
			num->setTitleColorForState(CAControl::State::Highlighted, CAColor4B::WHITE);
			subview->addSubview(num);

			CALabel* msg=CALabel::createWithLayout(DLayout(DHorizontalLayout_L_R(100, 200), DVerticalLayout_H_C(30, 0.75)));
			msg->setTag(400);
			msg->setFontSize(30);
			msg->setColor(CAColor4B::GRAY);
			msg->setTextAlignment(CATextAlignment::Left);
			msg->setVerticalTextAlignmet(CAVerticalTextAlignment::Center);
			subview->addSubview(msg);

			CALabel* datetime=CALabel::createWithLayout(DLayout(DHorizontalLayout_R_W(10, 130), DVerticalLayout_H_C(42, 0.75)));
			datetime->setTag(500);
			datetime->setFontSize(30);
			datetime->setColor(CAColor4B::GRAY);
			datetime->setTextAlignment(CATextAlignment::Right);
			datetime->setVerticalTextAlignmet(CAVerticalTextAlignment::Center);
			subview->addSubview(datetime);

			CAButton* btn=CAButton::createWithLayout(DLayout(DHorizontalLayout_R_W(0, 130), DVerticalLayoutFill), CAButton::Type::Custom);
			btn->setTag(600);
			btn->setBackgroundImageForState(CAControl::State::Normal, CAImage::create("img/button_delete.png"), false);
			cell->insertSubview(btn, -1);
		}
		cell->setDraggingLength(130);

		CSJson::Value val=datas[row];
		CAView* subview=cell->getContentView();
		CAImageView* portrait=(CAImageView*)subview->getSubviewByTag(100);
		portrait->setImage(CAImage::create(val["portrait"].asCString()));

		CALabel* name=(CALabel*)subview->getSubviewByTag(200);
		name->setText(val["name"].asCString());

		int count=val["num"].asInt();
		CAButton* num=(CAButton*)subview->getSubviewByTag(300);
		if(count==0){
			num->setTitleForState(CAControl::State::Normal, "");
		}else if(count<10){
			num->setTitleForState(CAControl::State::Normal, obj2str("%d", count));
			num->setLayout(DLayout(DHorizontalLayout_R_W(10, 42), DVerticalLayout_H_C(42, 0.35)));
			num->setBackgroundImageForState(CAControl::State::Normal, CAImage::create("img/remind1.png"), false);
			num->setBackgroundImageForState(CAControl::State::Highlighted, CAImage::create("img/remind1.png"), false);
		}else{
			if(count>99){
				num->setTitleForState(CAControl::State::Normal, "99+");
			}else{
				num->setTitleForState(CAControl::State::Normal, obj2str("%d", count));
			}
			num->setBackgroundImageForState(CAControl::State::Normal, CAImage::create("img/remind2.png"), false);
			num->setBackgroundImageForState(CAControl::State::Highlighted, CAImage::create("img/remind2.png"), false);
		}

		CALabel* msg=(CALabel*)subview->getSubviewByTag(400);
		msg->setText(val["msg"].asCString());

		CALabel* datetime=(CALabel*)subview->getSubviewByTag(500);
		datetime->setText(val["datetime"].asCString());

		CAButton* btn=(CAButton*)cell->getSubviewByTag(600);
		btn->addTarget([=]{
			CAAlertView* alert=CAAlertView::create("删除信息", "确定要继续该操作吗", {"确定", "取消"});
			alert->show([=](int index){
				CC_RETURN_IF(index==1);
				int size=datas.size();
				int j=-1;
				CSJson::Value values;
				values.resize(size-1);
				for(int i=0; i<size; i++){
					if(i==row){
						continue;
					}else{
						values[++j]=datas[i];
					}
				}
				datas=values;
				if(size>2){
					table->reloadData();
				}else{
					this->viewDidLoad();
				}
			});
		}, CAButton::Event::TouchDown);

		return cell;
	});
	//选中cell时触发
	table->onDidSelectCellAtIndexPath([&](unsigned int section, unsigned int row){
		CSJson::Value val=datas[row];
		getRootWindow()->pushViewController(ChatController::create(val["name"].asCString()), true);
	});

	this->fillData("data/messages.json");
}

void MessageController::fillData(const string& data){
	CSJson::Reader reader;
	CSJson::Value value;
	std::string json=FileUtils::getInstance()->getFileString(data);
	bool flag=reader.parse(json, value);
	if(!flag){
		getAlert(this->getView(), "数据解析异常, 请稍后重试");

		return;
	}
	datas=value["data"];

	table->reloadData();
}